using System;
using System.Linq;
using System.Text;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using System.Collections.Generic;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Utils;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Templates;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Model.NodeGenerators;
using System.ServiceModel;
using DevExpress.ExpressApp.Workflow.Xpo;
using DevExpress.ExpressApp.Workflow;
using WorkFlowDemo.Module.BusinessObjects;
using DevExpress.XtraEditors;


namespace WorkFlowDemo.Module.Win.Controllers
{
    [ServiceContract]
    public interface IStartStop
    {
        [OperationContract(IsOneWay = false)]
        [return: MessageParameterAttribute(Name="result")]
        string Start(String clientId);

        [OperationContract(IsOneWay = false)]
        [return: MessageParameterAttribute(Name = "result")]
        string Stop(string clientId);
        
    }

    // For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppViewControllertopic.
    public partial class ReceiveCorrelationsDemoController : ObjectViewController
    {
        private const string WorkflowServiceAddress = "http://localhost:19170/";
        private ParametrizedAction startWorkflowAction;
        private ParametrizedAction stopWorkflowAction;

        public IStartStop StartStopClient
        {
            get
            {
                IWorkflowDefinition definition = View.ObjectSpace.FindObject<XpoWorkflowDefinition>(CriteriaOperator.Parse("Name = 'Start/Stop (correlations) demo'"));
                if (definition != null)
                {
                    return ChannelFactory<IStartStop>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(WorkflowServiceAddress + definition.GetUniqueId()));
                }
                return null;
            }
        }

        public ReceiveCorrelationsDemoController()
        {
            InitializeComponent();
            RegisterActions(components);
            // Target required Views (via the TargetXXX properties) and create their Actions.

            TargetObjectType = typeof(Task);
            startWorkflowAction = new ParametrizedAction(this, "StartWorkflow", PredefinedCategory.Edit, typeof(string));
            startWorkflowAction.Execute += startWorkflowAction_Execute;

            stopWorkflowAction = new ParametrizedAction(this, "StopWorkflow", PredefinedCategory.Edit, typeof(string));
            stopWorkflowAction.Execute += stopWorkflowAction_Execute;
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            // Perform various tasks depending on the target View.
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            // Access and customize the target View control.
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();


        }

        void stopWorkflowAction_Execute(object sender, ParametrizedActionExecuteEventArgs e)
        {
            if (StartStopClient != null)
            {
                string result = StartStopClient.Stop((string)e.ParameterCurrentValue);
                XtraMessageBox.Show(result);
            }
        }

        void startWorkflowAction_Execute(object sender, ParametrizedActionExecuteEventArgs e)
        {
            if (StartStopClient != null)
            {
                string result = StartStopClient.Start((string)e.ParameterCurrentValue);
                XtraMessageBox.Show(result);
            }
        }
    }
}
